
var ServerHero = new Class({
	Extends: Hero,
	initialize: function(username){
            this.parent();
            this.username = username;
            this.oldStates = [];
            this.previousState = null;
            this.serverX=-1;
            this.serverY=-1;
            this.lastKeyCodeApplied=-1;
            this.lastCounterProcessed=0;
	},
        processKeyPressedInput: function(keyCode){
            this.lastKeyCodeApplied=keyCode;
            this.parent(keyCode);
        },
        processKeyUpInput: function(keyCode){
            this.lastKeyCodeApplied=keyCode;
            this.parent(keyCode);
        },
        processServerInput: function(keyCode,status){
            if(keyCode!=null){
                if(keyCode==-1)
                          this.processKeyUpInput(keyCode);
                 else{
                          if(keyCode==32)
                                  this.launchBomb();
                          else
                                  this.processKeyPressedInput(keyCode);
                 }
            }
            if(status!=null && keyCode!=null){
                this.updateState(status);
            }
        },
        checkIdleStatus: function(){
            this.parent();
            this.doServerActivity();
        },
        doServerActivity: function(){
            if(this.sense==0){
                if(this==game.hero)
                        sendKeyEventToServer(-1);
                else{
                    var deltaY=0;
                    if(this.serverY!=-1)
                            deltaY=Math.abs(this.y-this.serverY);
                    if(deltaY>this.cuanta){
                        this.y=this.serverY;
                        //this.HTMLIncarnation.setStyle('top',this.y);
                        this.HTMLIncarnation.style.top=this.y+'px';
                    }

                    var deltaX=0;
                    if(this.serverX!=-1)
                            deltaX=Math.abs(this.x-this.serverX);
                    if(deltaX>this.cuanta){
                        this.x=this.serverX;
                        //this.HTMLIncarnation.setStyle('left',this.x);
                        this.HTMLIncarnation.style.left=this.x+'px';
                    }
                }
              return;
            }
            if(this==game.hero)
                    sendKeyEventToServer(this.lastKeyCodeApplied); // we send the key event here because we do not want to send allevents generated between 2 calls of moveAlong
            // that don't get applied anyway to the hero, and save some bandwidth and request objects'
        },
        setSpriteImage: function(index){
            this.HTMLIncarnation.setStyle('background-image','url("img/allplayer'+index+'.png")')
        },
        updateState: function(status){
                var stateVars = status.split(':');
                if(this.lastCounterProcessed>=parseInt(stateVars[1]))
                    return;
                this.lastCounterProcessed=parseInt(stateVars[1]);
                this.serverX=parseInt(stateVars[2]);
                this.serverY=parseInt(stateVars[3]);
                return;
                var stateVars = status.split(':');
                  var newState = new HeroState(stateVars[4],stateVars[5],stateVars[2],stateVars[3],stateVars[1],stateVars[6],stateVars[7]);
                  var previousState = null;
                  var previousStateIndex = this.oldStates.length-1;
                  for(var i=this.oldStates.length-1;i>=0;i--){
                      if(this.oldStates[i].stateCounter!=null){
                          previousStateIndex=i;
                          previousState = this.oldStates[i];
                          break;
                      }
                  }
                if(previousState!=null){
                    var previewedState = previousStateIndex + this.oldStates[newState.stateCounter - previousState.stateCounter];
                    if(previewedState.equals(newState)){
                        // ok
                    } else {
                        // make adjustments
                        debugger;
                        this.x=parseInt(newState.x);
                        this.y=parseInt(newState.y);
                        this.currentDirection=newState.currentDirection;
                        this.sense = newState.sense;
                        this.cuanta = newState.cuanta;
                        this.life=newState.life;
                    }
                } else {
                    debugger;
                }
                  if(this.oldStates.length>game.stateHistorySize){
                      this.oldStates.shift();
                  }
                  this.oldStates.push(newState);

        }
});